﻿@using Nop.Services.Catalog
@model ProductModel


@if (Model.Id > 0)
{
    if (Model.NumberOfAvailableCategories > 0)
    {
        <div id="productcategories-grid"></div>

        <script>
            $(document).ready(function () {
                $("#productcategories-grid").kendoGrid({
                    dataSource: {
                        type: "json",
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ProductCategoryList", "Product", new { productId = Model.Id }))",
                                type: "POST",
                                dataType: "json"
                            },
                            create: {
                                url: "@Html.Raw(Url.Action("ProductCategoryInsert", "Product", new { productId = Model.Id }))",
                                type: "POST",
                                dataType: "json"
                            },
                            update: {
                                url:"@Html.Raw(Url.Action("ProductCategoryUpdate", "Product"))",
                                type: "POST",
                                dataType: "json"
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ProductCategoryDelete", "Product"))",
                                type: "POST",
                                dataType: "json"
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id",
                                fields: {
                                    Category: { editable: true, type: "string" },
                                    //ProductId: { editable: false, type: "number" },
                                    CategoryId: { editable: true, type: "number" },
                                    IsFeaturedProduct: { editable: true, type: "boolean" },
                                    DisplayOrder: { editable: true, type: "number" },
                                    Id: { editable: false, type: "number" }
                                }
                            }
                        },
                        requestEnd: function (e) {
                            if (e.type == "create" || e.type == "update") {
                                this.read();
                            }
                        },
                        error: function (e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        numeric: false,
                        previousNext: false,
                        info: false
                    },
                    toolbar: ["create"],
                    edit: function(e) {
                        if (e.model.isNew()) {
                            //little hack here
                            //pre-select the first value of kendoui dropdownlist datasource
                            //for some reasons (maybe, bug) it always sends 0
                            //if no value has been selected (changed) in the dropdownlist
                            if (allCategories.length > 0) {
                                e.model.CategoryId = allCategories[0].Id;
                            }
                        }
                    },
                    editable: {
                        confirmation: false,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [{
                            field: "CategoryId",
                            title: "@T("Admin.Catalog.Products.Categories.Fields.Category")",
                            width: 200,
                            editor: categoryDropDownEditor, 
                            template: "#:Category#"
                        },
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            //vendor cannot edit this field
                            <text>{
                            field: "IsFeaturedProduct",
                            title: "@T("Admin.Catalog.Products.Categories.Fields.IsFeaturedProduct")",
                            width: 200,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsFeaturedProduct#.gif" />'
                    },</text>
                        }
                        
                    {
                        field: "DisplayOrder",
                        title: "@T("Admin.Catalog.Products.Categories.Fields.DisplayOrder")",
                        width: 200,
                        //integer format
                        format: "{0:0}"
                    }, {
                        command: [{
                            name: "edit",
                            text: "@T("Admin.Common.Edit")"
                        }, {
                            name: "destroy",
                            text: "@T("Admin.Common.Delete")"
                        }],
                        width: 200
                    }]
                });
            });
                
            //local datasource
            var allCategories = [
                @{
                    var categoryService = EngineContext.Current.Resolve<ICategoryService>();
                    var allCategories = categoryService.GetAllCategories(showHidden: true);
                }
                @for (int i = 0; i < allCategories.Count; i++)
                {
                    var category = allCategories[i];
                    <text>
                        {
                            Id: @(category.Id),
                            Name: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(category.GetFormattedBreadCrumb(allCategories))))"
                        }
                        </text>
                    if (i != allCategories.Count - 1)
                    {
                        <text>,</text>
                    }
                }
            ];

            function categoryDropDownEditor(container, options) {
                $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:CategoryId"/>')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: false,
                        dataSource: allCategories
                    });
            }
        </script>
    }
    else
    {
    @T("Admin.Catalog.Products.Categories.NoCategoriesAvailable")
    }
}
else
{
    @T("Admin.Catalog.Products.Categories.SaveBeforeEdit")
}

